package gold.enterprise;

public class ByteDanceHCK {


    /**
     * 题目：一个省有100万考生成绩，给定一个组需要排名的数组，找其排名
     *
     * @param allScore
     * @param toBeRanked
     * @return
     */
    public int[] getRank(int[] allScore, int[] toBeRanked) {
        int[] scoreBucket = new int[751];
        for (int score : allScore) {
            scoreBucket[score]++;//累加桶
        }

        int[] rank = new int[toBeRanked.length];
        for (int i = 0; i < toBeRanked.length; i++) {
            int score = toBeRanked[i];
            rank[i] = findRank(scoreBucket, score);

        }
        return rank;
    }

    // 此函数可以用前缀和优化
    public int findRank(int[] scoreBucket, int score) {
        int sum = 0;
        for (int i = scoreBucket.length - 1; i >= 0; i--) {
            sum += scoreBucket[i];
            if (score > i) return sum;
        }

        return sum;
    }

}
